package com.example.tuter.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.adapter.NotificationAdapter;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.Notification;
import com.example.tuter.util.Constant;
import com.example.tuter.util.httpUtil.HttpFunction.UserAPI;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;

public class NotificationActivity extends BaseActivity {

    String TAG = NotificationActivity.class.getSimpleName();

    private static ArrayList<Notification> notifications = new ArrayList<>();
    private NotificationAdapter notificationAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();




        if (!notifications.isEmpty()) {
            //将所有消息都置为已读
            ackMsg(notifications.get(0).getMessageId());
        }


    }


    public void ackMsg(String msgID) {
        try {

            UserAPI userAPI = Constant.r.create(UserAPI.class);
            Call<Message<String>> call = userAPI.ackMessage(msgID);
            call.enqueue(new Callback<Message<String>>() {
                @Override
                public void onResponse(Call<Message<String>> call, retrofit2.Response<Message<String>> response) {
                    //成功
                }

                @Override
                public void onFailure(Call<Message<String>> call, Throwable t) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void initData() {
        //获取本地数据库中的所有消息
    }

    @Override
    protected void bindEvent() {
        //设置点击事件
        notificationAdapter.setRecyclerItemClickListener(new NotificationAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {

                Intent intent = new Intent(NotificationActivity.this, ViewPostActivity.class);
                intent.putExtra("notification", notifications.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_unread_msg);


        RecyclerView recyclerView = findViewById(R.id.activity_unread_recyclerView);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);


        notificationAdapter = new NotificationAdapter(notifications, this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(notificationAdapter);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_unread_msg;
    }
}
